#include "../common/particle_circle.h"

namespace Packing_common
{
	void Particle_circle::gl_draw(const GLfloat *color) const
	{
		const unsigned int nb_pieces = 50;
		std::vector<Point_2> approx_pgn;
		approx_pgn.reserve(nb_pieces);
		double step = boost::math::constants::two_pi<double>()/nb_pieces;
		double r = radius();
		for (unsigned int i = 0; i < nb_pieces; i++)
		{
			double angle = i*step;
			Point_2 p = center() + r*Vector_2(std::cos(angle), std::sin(angle));
			approx_pgn.push_back(p);
		}

		// draw outline
		glColor3f(0.1f, 0.1f, 0.1f);
		glLineWidth(0.5f);
		glBegin(GL_LINE_LOOP);
		for (unsigned int i = 0; i < nb_pieces; i++)
			glVertex2d(approx_pgn[i].x(), approx_pgn[i].y());
		glEnd();

		// fill
		glColor3fv(color);
		glBegin(GL_POLYGON);
		for (unsigned int i = 0; i < nb_pieces; i++)
			glVertex2d(approx_pgn[i].x(), approx_pgn[i].y());
		glEnd();
	}
	
	void Particle_circle::translate(double tx, double ty)
	{
		CGAL_Circle_2::operator=(CGAL_Circle_2(center() + Vector_2(tx, ty), squared_radius()));
	}

	Particle_circle centralized_transform(const Particle_circle& c, /*const Particle_circle::Point_2& ref_pnt,*/ double theta, double tx, double ty)
	{
		typedef CGAL::Aff_transformation_2< CGAL::Exact_predicates_inexact_constructions_kernel > Transformation_2;
		
		//Particle_circle::Point_2 cent = c.center();
		//Transformation_2 to_origin(CGAL::TRANSLATION, Particle_circle::Vector_2(ref_pnt, CGAL::ORIGIN));
		double fs = std::sin(theta), fc = std::cos(theta);
		Transformation_2 rot_and_trans(fc, -fs, tx, fs, fc, ty);
		//Transformation_2 back(CGAL::TRANSLATION, Particle_circle::Vector_2(CGAL::ORIGIN, ref_pnt));
		//Transformation_2 t = back*(rot_and_trans*to_origin);
		Particle_circle pc(rot_and_trans(c.center()), c.squared_radius());
		Particle_circle::Vector_2 nm = rot_and_trans(c.orientation_vector());
		if (nm.x() != 0.0 || nm.y() != 0.0)
			nm = nm / CGAL::sqrt(nm.squared_length());
		pc.orientation_vector(nm);
		return pc;

	}
}